package com.army.study.ui;

import com.army.study.ResourceTable;
import com.army.study.data.DeviceData;
import com.army.study.ui.listcomponent.CommentViewHolder;
import com.army.study.ui.listcomponent.ListComponentAdapter;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class DeviceSelectDialog extends CommonDialog {
    private static final int CORNER_RADIUS = 10;
    ListContainer mListContainer;
    List<DeviceData> mDeviceList = new ArrayList<>();
    ListComponentAdapter listComponentAdapter;
    private DeviceInfo mCheckedDevice;

    private Context mContext;
    private OnclickListener mOnclickListener;

    public interface OnclickListener {
        void onYesClick(DeviceInfo deviceInfo);
    }

    public DeviceSelectDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public void setListener(OnclickListener listener) {
        mOnclickListener = listener;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        Component rootView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_layout_device, null, false);
        mListContainer = (ListContainer)rootView.findComponentById(ResourceTable.Id_list_container_device);
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        mDeviceList.clear();
        for (DeviceInfo deviceInfo : deviceInfoList) {
            mDeviceList.add(new DeviceData(false, deviceInfo));
        }
        if (deviceInfoList.size() > 0) {
            mCheckedDevice = deviceInfoList.get(0);
        }

        setItemProvider();

        Text operateYes = (Text)rootView.findComponentById(ResourceTable.Id_operate_yes);
        operateYes.setClickedListener(component -> {
            if (mOnclickListener != null && mCheckedDevice != null) {
                mOnclickListener.onYesClick(mCheckedDevice);
            }
        });

        Text operateNo = (Text)rootView.findComponentById(ResourceTable.Id_operate_no);
        operateNo.setClickedListener(component -> {
            hide();
        });

        setSize(MATCH_PARENT, MATCH_CONTENT);
        setAlignment(LayoutAlignment.BOTTOM);
        setCornerRadius(CORNER_RADIUS);
        setAutoClosable(true);
        setContentCustomComponent(rootView);
        setTransparent(true);
    }

    private void setItemProvider() {
        listComponentAdapter = new ListComponentAdapter<DeviceData>(mContext, mDeviceList, ResourceTable.Layout_dialog_device_item) {
            @Override
            public void onBindViewHolder(CommentViewHolder commentViewHolder, DeviceData item, int position) {
                commentViewHolder.getTextView(ResourceTable.Id_item_desc)
                        .setText(item.getDeviceInfo().getDeviceName());

                switch (item.getDeviceInfo().getDeviceType()) {
                    case SMART_PHONE:
                        commentViewHolder.getImageView(ResourceTable.Id_item_type)
                                .setPixelMap(ResourceTable.Media_dv_phone);
                        break;
                    case SMART_PAD:
                        commentViewHolder.getImageView(ResourceTable.Id_item_type)
                                .setPixelMap(ResourceTable.Media_dv_pad);
                        break;
                    case SMART_WATCH:
                        commentViewHolder.getImageView(ResourceTable.Id_item_type)
                                .setPixelMap(ResourceTable.Media_dv_watch);
                        break;
                    default:
                        break;
                }

                commentViewHolder.getImageView(ResourceTable.Id_item_check)
                        .setPixelMap(item.isChecked()
                            ? ResourceTable.Media_checked_point
                            : ResourceTable.Media_uncheck_point);
            }

            @Override
            public void onItemClick(Component component, DeviceData deviceData, int i) {
                super.onItemClick(component, deviceData, i);
                for (DeviceData mDevice : mDeviceList) {
                    mDevice.setChecked(false);
                }
                mDeviceList.get(i).setChecked(true);
                listComponentAdapter.notifyDataChanged();
                mCheckedDevice = deviceData.getDeviceInfo();
            }
        };
        mListContainer.setItemProvider(listComponentAdapter);
    }
}
